package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.ReturnOrderItemMapper;
import com.kang.dao.ReturnOrderMapper;
import com.kang.dao.ReturnRequestItemMapper;
import com.kang.dao.SupplyAgreementItemMapper;
import com.kang.pojo.ReturnOrder;
import com.kang.pojo.ReturnOrderItem;
import com.kang.pojo.ReturnRequestItem;
import com.kang.pojo.SupplyAgreementItem;
import com.kang.service.ReturnOrderService;
import com.vonechina.entity.page.ReturnAgentPage;
import com.vonechina.entity.page.ReturnOrderPage;
import com.vonechina.entity.page.ReturnRequestItemPage;

/**
 * 退回订单服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class ReturnOrderServiceImpl implements ReturnOrderService {
	/**
	 * 退回申请物品信息持久层
	 */
	@Resource
	private ReturnRequestItemMapper rrim;
	/**
	 * 退货订单持久层
	 */
	@Resource
	private ReturnOrderMapper rom;
	/**
	 * 退货订单物品信息持久层
	 */
	@Resource
	private ReturnOrderItemMapper roim;
	/**
	 * 供货洗衣物品信息持久层
	 */
	@Resource
	private SupplyAgreementItemMapper saim;

	/**
	 * 分页查询退货订单
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<ReturnOrder> listReturnOrder(ReturnOrderPage page) {
		page.setRows(rom.countReturnOrder(page));
		return rom.listReturnOrder(page);
	}

	/**
	 * 更新退货订单
	 * 
	 * @param order
	 *            退货订单实体类
	 * @return
	 */
	@Override
	public Integer updateReturnOrder(ReturnOrder order) {
		return rom.updateByPrimaryKeySelective(order);
	}

	/**
	 * 删除退货订单
	 * 
	 * @param order
	 *            退货订单实体类
	 * @return
	 */
	@Override
	public Integer deleteReturnOrder(ReturnOrder order) {
		int a = rom.deleteByPrimaryKey(order.getReturnOrderID());
		if (a > 0) {
			roim.updateRequestItemStatus(order.getReturnOrderID(), 0);
			roim.deleteOrderItem(order.getReturnOrderID());
		}
		return a;
	}

	/**
	 * 根据ID查询退货订单
	 * 
	 * @param returnOrderID
	 *            退货订单ID
	 * @return
	 */
	@Override
	public ReturnOrder selectByID(Integer returnOrderID) {
		return rom.selectByPrimaryKey(returnOrderID);
	}

	/**
	 * 查询可退货物品批次
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<ReturnRequestItem> listReturnRequestItem(ReturnAgentPage page) {
		page.setRows(rrim.countReturnRequestItem(page));
		return rrim.listReturnRequestItem(page);
	}

	/**
	 * 新增退货订单
	 * 
	 * @param order
	 *            退货订单实体类
	 * @return
	 */
	@Override
	public Integer addReturnOrder(ReturnOrder order) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = rom.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		order.setReturnNumber("RO" + no);
		return rom.insertSelective(order);
	}

	/**
	 * 提交退货订单
	 * 
	 * @param order
	 *            退货订单实体类
	 * @return
	 */
	@Override
	public Integer submitReturnOrder(ReturnOrder order) {
		return rom.submitReturnOrder(order);
	}

	/**
	 * 搜索符合条件可退货物品批次
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<ReturnRequestItem> listReturnRequestItemNotIn(ReturnRequestItemPage page) {
		page.setRows(rrim.countNotInItem(page));
		return rrim.listNotInItem(page);
	}

	/**
	 * 保存退货订单物品信息
	 * 
	 * @param order
	 *            退货订单ID
	 * @param sourceID
	 *            退货物品来源ID
	 * @return
	 */
	@Override
	public Integer saveReturnOrderItem(ReturnOrder order, Integer[] sourceIDs) {
		roim.updateRequestItemStatus(order.getReturnOrderID(), 0);
		roim.deleteItemNotIn(order.getReturnOrderID(), sourceIDs);
		for (Integer id : sourceIDs) {
			if (roim.selectItem(order.getReturnOrderID(), id) == null) {
				ReturnRequestItem returnRequestItem = rrim.selectByPrimaryKey(id);
				ReturnOrderItem item = new ReturnOrderItem();
				item.setReturnOrderID(order.getReturnOrderID());
				item.setWarehouseManagementID(order.getWarehouseManagementID());
				item.setReturnQuantity(returnRequestItem.getNumber());
				item.setSourceID(id);
				item.setItemInformationID(returnRequestItem.getItemInformationID());
				roim.insertSelective(item);
			}
		}
		return roim.updateRequestItemStatus(order.getReturnOrderID(), 1);
	}

	/**
	 * 查询退货订单物品信息
	 * 
	 * @param returnOrderID
	 *            退货订单ID
	 * @return
	 */
	@Override
	public List<ReturnOrderItem> listOrderItem(Integer returnOrderID) {
		return roim.listOrderItem(returnOrderID);
	}

	/**
	 * 计算退货订单金额
	 * 
	 * @param order
	 *            退货订单实体类
	 * @return
	 */
	@Override
	public Double countOrderPrice(ReturnOrder order) {
		double price = 0.0;
		List<ReturnOrderItem> list = roim.listOrderItem(order.getReturnOrderID());
		for (ReturnOrderItem item : list) {
			SupplyAgreementItem agreementItem = saim.selectItemBySupplier(order.getSupplierID(),
					item.getItemInformationID());
			price += item.getReturnQuantity() * agreementItem.getPurchaseprice();
		}
		return price;
	}

	/**
	 * 结算退货订单
	 * 
	 * @param order
	 *            退货订单实体类
	 * @return
	 */
	@Override
	public Integer settleOrder(ReturnOrder order) {
		ReturnOrder o = new ReturnOrder();
		o.setActualReceive(order.getActualReceive());
		o.setShouldReceive(order.getShouldReceive());
		o.setIsSettle(1);
		o.setSettlePersonID(order.getSettlePersonID());
		o.setSettleTime(new Date());
		o.setReturnOrderID(order.getReturnOrderID());
		return rom.updateByPrimaryKeySelective(o);
	}
}
